// miniprogram/pages/redeem/index.js
Page({
  data: {
    userInfo: {},
    totalPoints: 1250,
    redeemItems: [],
    filteredItems: [],
    sceneType: '',
    currentCategory: 'all'
  },

  onLoad() {
    const app = getApp()
    this.setData({
      userInfo: app.globalData.userInfo,
      sceneType: app.globalData.sceneType
    })
    this.loadRedeemItems()
  },

  onShow() {
    // 页面显示时刷新数据
    this.loadRedeemItems()
  },

  loadRedeemItems() {
    // 模拟积分兑换商品数据
    const redeemItems = [
      { 
        id: 1, 
        name: '星巴克咖啡券', 
        description: '任意规格星巴克咖啡一杯', 
        points: 50, 
        image: '/images/redeem_starbucks.png',
        stock: 10,
        category: '餐饮'
      },
      { 
        id: 2, 
        name: '电影票', 
        description: '任意影院2D电影票一张', 
        points: 80, 
        image: '/images/redeem_movie.png',
        stock: 5,
        category: '娱乐'
      },
      { 
        id: 3, 
        name: '10元话费充值', 
        description: '中国移动/联通/电信话费充值', 
        points: 100, 
        image: '/images/redeem_phone.png',
        stock: 20,
        category: '通讯'
      },
      { 
        id: 4, 
        name: '精美笔记本', 
        description: '高品质皮质笔记本一本', 
        points: 150, 
        image: '/images/redeem_notebook.png',
        stock: 3,
        category: '办公'
      },
      { 
        id: 5, 
        name: '蓝牙耳机', 
        description: '品牌蓝牙耳机一副', 
        points: 300, 
        image: '/images/redeem_headset.png',
        stock: 2,
        category: '电子'
      }
    ]
    
    this.setData({ 
      redeemItems,
      filteredItems: redeemItems
    })
  },

  switchCategory(e) {
    const category = e.currentTarget.dataset.category
    let filteredItems = []
    
    if (category === 'all') {
      filteredItems = this.data.redeemItems
    } else {
      filteredItems = this.data.redeemItems.filter(item => item.category === category)
    }
    
    this.setData({
      currentCategory: category,
      filteredItems
    })
  },

  goToRedeemDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/redeem/detail?id=${id}`
    })
  },

  goToRedeemHistory() {
    wx.navigateTo({
      url: '/pages/redeem/history'
    })
  }
})